<template>
    <div class="box">
        <!--小球-->
        <transition 
        @before-enter="beforeEnter" 
        @enter="enter" 
        @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag"></div>
        </transition>
        
        <!--轮播-->
        <div class="mui-card">
            <div class="mui-card-content">
                <swiper :swiperList="imgList" :isfull="false"></swiper>
            </div>
        </div>

        <!--商品信息-->
        <div class="mui-card">
            <div class="mui-card-header">{{info.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-inner">
                    <p>市场价：<del>￥{{info.market_price}}</del>&nbsp;销售价：<span>￥{{info.sell_price}}</span></p>
                    <p>购买数量：<numbox @getCount="getSelectedCount" :max='info.stock_quantity'></numbox></p>
                    
                    <mt-button type="primary">立即购买</mt-button>
                    <mt-button type="danger" @click="addToShopCar">加入购物车</mt-button>
                </div>
            </div>
        </div>

        <!--商品参数-->
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <p>商品货号：{{info.good_no}}</p>
                <p>库存情况：{{info.stock_quantity}}件</p>
                <p>上架时间：{{info.add_time|dateFormat}}</p>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComments(id)">商品评论</mt-button>
            </div>
        </div>

    </div>
</template>

<script>
import swiper from '../subcomponents/swiper.vue'
import numbox from './numbox.vue'
export default {
    data(){
        return{
            imgList:[],
            id:this.$route.params.id,
            info:{},
            ballFlag:false,
            selectedCount:1,
            imax:99,
            num: this.$store.getters.getAllCount
        }
    },
    components:{
        swiper,
        numbox
    },
    created(){
        this.getImg();
        this.getInfo();
        //console.log(this.id);
    },

    methods:{
        getImg(){
            this.$http.get('static/data.json').then(res=>{
                res.body.data.goodimg.forEach(item => {
                    item.imgUrl = item.src
                    if(item.id==this.id){
                         this.imgList.push(item)
                    }
                });
            })
        },
        getInfo(){
            this.$http.get('static/data.json').then(res=>{
                res.body.data.goods.forEach(item=>{
                    if(item.id==this.id){
                        this.info = item;
                    }
                })
            })
        },
        goDesc(id){
            this.$router.push({name:'desc',params:{id}})
        },
        goComments(id){
            this.$router.push({name:'goodscomment',params:{id}})
        },
        getSelectedCount(count){
            this.selectedCount = count;
            return this.selectedCount;
        },
        addToShopCar(){
            this.ballFlag = !this.ballFlag
            var goodsinfo = {
                id:this.id,
                count:parseInt(this.$store.state.num),
                price:this.info.sell_price,
                title:this.info.title,
                img:this.info.img_url,
                selected:true
            }
            console.log(goodsinfo.count)
            //this.$store.commit('addToCar',goodsinfo);
            this.$store.commit("addToCar", goodsinfo);
        },
        beforeEnter(el){

            var ball = document.getElementById('test').getBoundingClientRect();
            el.style.left = ball.left+'px';
            el.style.top = ball.top + document.documentElement.scrollTop+'px';
            el.style.transform = "translate(0, 0)";
        },
        enter(el,done){
            el.offsetWidth;
            //小球的位置
            const ballPosition = el.getBoundingClientRect();
            //徽标位置
            const badge = document.getElementById('badge').getBoundingClientRect();
            var left,top;
            left = badge.left-ballPosition.left;
            top = badge.top-ballPosition.top;
            el.style.transform=`translate(${left}px,${top}px)`;
            el.style.transition = 'all 1s cubic-bezier(.4,-0.3,1,.68)'
            done()
        },
        afterEnter(el){
            this.ballFlag = !this.ballFlag
        },
    }
    
}
</script>

<style scoped>
.ball{
    width: 15px;
    height: 15px;
    border-radius:50%;
    background:red;
    position:absolute;
    z-index:1100;
    /*top:47%;
    left: 32%;*/
}
.mui-card-footer{
    display: block;
}
button{
    margin-bottom: 15px;
};
.box{
    background: #eee;
    overflow: hidden;
}
</style>
